package cn.itsource.controller;

import cn.itsource.domain.Article;
import cn.itsource.dto.ArticleDto;
import cn.itsource.exception.BusinessException;
import cn.itsource.query.ArticleQuery;
import cn.itsource.service.ArticleService;
import cn.itsource.util.AjaxResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 接口类：文章Article
 */
@RestController
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    /**
     * 查询所有
     * @return
     */
    //注意前端地址：http://localhost:8080/article - get
    @GetMapping
    public List<Article> findAll() {
        return articleService.findAll();
    }

    /**
     * 查询单个
     * @param id
     * @return
     */
    //注意前端地址：http://localhost:8080/article/1 - get
    @GetMapping("/{id}")
    public Article findOne(@PathVariable("id") Long id) {
        return articleService.findOne(id);
    }

    /**
     * 添加或修改
     * @param article
     * @return
     */
    //注意前端地址：http://localhost:8080/article - put - {}
    //如果前端是表单数据：Article article。但是现在vue中用的axios发请求-多个参数都会使用json格式进行传递
    //前端传递的是json数据，后端必须要用一个注解：@RequestBody
    @PutMapping
    public AjaxResult addorUpdate(@RequestBody Article article) {
        try {
            if (article.getId() == null) {//添加操作
                articleService.add(article);
            } else {
                articleService.update(article);
            }
            //如果成功
            return new AjaxResult();
        } catch (BusinessException e) {
            return new AjaxResult(false, e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "网络繁忙,请稍后重试");
        }
    }

    /**
     * 删除
     * @param id
     * @return
     */
    //注意前端地址：http://localhost:8080/article/1 - delete
    @DeleteMapping("{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
        try {
            articleService.delete(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "网络繁忙,请稍后重试");
        }
    }

    /**
     * 分页查询 + 高级查询
     * @param query
     * @return
     */
    @PostMapping
    public PageInfo<Article> pageQuery(@RequestBody ArticleQuery query) {

        PageInfo<Article> pageInfo = articleService.pageQuery(query);
        //PageInfo【total ，list】- 前端获取
        return pageInfo;
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PatchMapping
    public AjaxResult patchDel(@RequestBody Long[] ids) {  //["2","14"] - Long[] ids
        try {
            articleService.patchDel(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "网络繁忙,请稍后重试");
        }
    }

    /**
     * 前台：根据文章类型统计发布的文章数量
     * @return
     */
    @GetMapping("/countByArticleType")
    public List<ArticleDto> countByArticleType() {
        return articleService.countByArticleType();
    }

    /**
     * 前台：根据时间统计发布的文章数量
     * @return
     */
    @GetMapping("/countByTime")
    public List<ArticleDto> countByTime() {
        return articleService.countByTime();
    }

    /**
     * 前台：获取相关文章
     * @param id
     * @return
     */
    @GetMapping("/getDependencyArticle/{id}")
    public List<Article> getDependencyArticle(Long id) {
        return articleService.getDependencyArticle(id);
    }

    /**
     * 后台首页Echarts：统计点赞前6的文章
     * @return
     */
    @GetMapping("/chartColumn")
    public List<ArticleDto> getChartColumn() {
        return articleService.getChartColumn();
    }

    /**
     * 后台首页Echarts：统计文章数量最多的文章类型
     * @return
     */
    @GetMapping("/chartPie")
    public List<ArticleDto> getChartPie() {
        return articleService.getChartPie();
    }

    /**
     * 前台：修改阅读数量
     * @param id
     * @return
     */
    @PutMapping("/readCount/{id}")
    public AjaxResult readCount(@PathVariable("id")Long id){
        return articleService.readCount(id);
    }

    /**
     * 前台：根据文章类型获取文章列表
     * @param typeId
     * @return
     */
    @GetMapping("/getArticleByType/{typeId}")
    public Map<String,Object> getArticleByType(@PathVariable("typeId")Long typeId){
        return articleService.getArticleByType(typeId);
    }
}
